package Modele;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class Bibliotheque extends Observable{

	private Vector<Repertoire> rep;
	private Vector<Album> alb;
        private String fichier;
	
	public Bibliotheque(){
		rep = new Vector();
		alb = new Vector();
                fichier = "library.xml";
	}
        
        public Bibliotheque(Album album){
            super();
            ajouterAlbum(album);
        }
        
        public Bibliotheque(Repertoire rep){
            super();
            ajouterRepertoire(rep);
        }
        
        public Bibliotheque(Album al, Repertoire rep){
            super();
            ajouterAlbum(al);
            ajouterRepertoire(rep);
        }
        
        public Bibliotheque(Vector<Album> al, Vector<Repertoire> rep){
            this.rep = rep;
            alb = al;
        }
        
	public void ajouterRepertoire(Repertoire r){
		rep.addElement(r);		
	}
	
	public void ajouterAlbum(Album a){
		alb.addElement(a);
	}
	
	public void supprimerRepertoire(Repertoire r){
	    //rep.remove(r);
	    Iterator it=rep.iterator();
	    while (it.hasNext())
            {
		    Repertoire rep2=(Repertoire)(it.next());
		    if (rep2.equals(r))
			{
			    rep.remove(rep2);
			}
            }
	}

        public boolean existeRep(String adr){
            Iterator it = rep.iterator();
            while(it.hasNext()){
                Repertoire tmp = (Repertoire) it.next();
                if (tmp.equals(adr)){
                    return true;
                }
            }
            
            
            return false;
        }
   
        public boolean existeAl(String adr){
            Iterator it = alb.iterator();
            while(it.hasNext()){
                Album tmp = (Album) it.next();
                if (tmp.equals(adr)){
                    return true;
                }
            }
            
            
            return false;
        }
	
	public void supprimerAlbum(Album a){
		alb.remove(a);
	}

	public Vector getRepertoire(){
		return rep;
	}
        
        public Repertoire[] getRepertoires(){
            Repertoire[] repertoires=new Repertoire[rep.size()];
            Iterator it=rep.iterator();
            int i=0;
            while(it.hasNext())
            {
                Repertoire cur=(Repertoire)(it.next());
                repertoires[i]=cur;
                i++;
            }
            return repertoires;
        }
	
	public Vector getAlbum(){
		return alb;
	}
	
    public Album[] getAlbums(){
            Album[] albums=new Album[alb.size()];
            Iterator it=alb.iterator();
            int i=0;
            while(it.hasNext())
            {
                Album cur=(Album)(it.next());
                albums[i]=cur;
                i++;
            }
            return albums;
        }

	public void telechargerImage(String url){
		
	}
	
	public void addImEnsIm(ModeleImage img, EnsembleImage ens_im){
		if (ens_im instanceof Repertoire)
                {
                    ((Repertoire)(rep.elementAt(rep.indexOf(ens_im)))).ajouterImage(img);
                }
                else
                {
                    ((Album)(alb.elementAt(alb.indexOf(ens_im)))).ajouterImage(img);
                }
	}
        
        public void remImEnsIm(ModeleImage img, EnsembleImage ens_im)
        {
            if (ens_im instanceof Repertoire)
                {
                    ((Repertoire)(rep.elementAt(rep.indexOf(ens_im)))).supprimerImage(img);
                }
                else
                {
                    ((Album)(alb.elementAt(alb.indexOf(ens_im)))).supprimerImage(img);
                }
        }


        public int getSize()
        {
            return rep.size();
        }
		
        public void saveLibrary() throws IOException{
            XStream xstream = new XStream(new DomDriver());
            
            FileWriter fw = new FileWriter(fichier);
            
            try{
                xstream.omitField(Observable.class, "Observable");
                xstream.toXML(this, fw);
                //System.out.println(xml);
                fw.close();
            }finally{
                fw.close();
            }
            
        }
 
}
